// pages/list/list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 请求数据是否有值（添加或被添加）
    currentTab: 0,
    setinterval: '',
    have_data: false,
    current: null,
    currentFriend: null,
    start: null,
    end: null,
    startFriend: null,
    endFriend: null,
    list: [],
    haoyou_list: [],
    footer: {
      footdex: 0,
      txtcolor: "#A2A2A2",
      seltxt: "#EC6464",
      background: "#fff",
      list: []
    }
  },
  switch_tab(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
  },
  look_detail(e) {
    var touxiang1 = e.currentTarget.dataset.docimg
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/kefu_duihua/kefu_duihua?t_id=' + e.currentTarget.dataset.tid + '&f_id=' + e.currentTarget.dataset.fid + '&name=' + e.currentTarget.dataset.name + '&touxiang2=' + e.currentTarget.dataset.avatar + '&touxiang1=' + touxiang1 + '&docimg=' + e.currentTarget.dataset.avatar + '&docid=' + e.currentTarget.dataset.docid + '&ifkf=1' + '&kfid=' + e.currentTarget.dataset.kfid + '&f_name=' + e.currentTarget.dataset.f_name,
    })
  },
  del(e) {
    var that = this;
    var list = that.data.list;
    var index = e.currentTarget.dataset.id;
    var fid = e.currentTarget.dataset.fid;
    var tid = e.currentTarget.dataset.tid;
    // console.log("aaa")
    wx.showModal({
      title: '提示',
      content: '确定删除和TA的所有聊天信息',
      success: function(res) {
        if (res.confirm) {
          //删除会话信息
          app.util.request({
            'url': 'entry/wxapp/DeleteMsg',
            'data': {
              openid: wx.getStorageSync('openid'),
              fid: fid,
              tid: tid
            },
            success: function(ress) {
              console.log(ress)
              list.splice(index, 1);
              that.setData({
                list: list,
                current: null
              })
            },
          })
        }
      }
    })
  },
  start(e) {
    this.setData({
      start: e.changedTouches[0].pageX
    })
  },
  move(e) {

  },
  end(e) {
    // console.log(e)
    this.setData({
      end: e.changedTouches[0].pageX
    })
    if (this.data.start - this.data.end > 0) {
      this.setData({
        current: e.currentTarget.dataset.id,
      })
    } else if (this.data.start - this.data.end < 0) {
      this.setData({
        current: null,

      })
    }
  },
  delFriend(e) {
    var that = this;
    var list = that.data.haoyou_list;
    var index = e.currentTarget.dataset.id;
    var fid = e.currentTarget.dataset.fid;
    var tid = e.currentTarget.dataset.tid;
    // console.log("aaa")
    wx.showModal({
      title: '提示',
      content: '确定删除好友和TA的所有聊天信息',
      success: function(res) {
        if (res.confirm) {
          //删除会话信息
          app.util.request({
            'url': 'entry/wxapp/DeleteHaoyou',
            'data': {
              openid: wx.getStorageSync('openid'),
              fid: fid,
              tid: tid
            },
            success: function(ress) {
              console.log(res)
              list.splice(index, 1);
              that.setData({
                haoyou_list: list,
                currentFriend: null
              })
            },
          })
        }
      }
    })
  },
  startFriend(e) {
    this.setData({
      startFriend: e.changedTouches[0].pageX
    })
  },
  moveFriend(e) {

  },
  endFriend(e) {
    // console.log(e)
    this.setData({
      endFriend: e.changedTouches[0].pageX
    })
    if (this.data.startFriend - this.data.endFriend > 0) {
      this.setData({
        currentFriend: e.currentTarget.dataset.id,
      })
    } else if (this.data.startFriend - this.data.endFriend < 0) {
      this.setData({
        currentFriend: null,

      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this;
    if (options.index != undefined) {
      var footer = that.data.footer;
      footer.footdex = options.index;
      that.setData({
        footer: footer
      })
    }
    that.getChatList();
    //循环查询对方发送的消息 把最新的消息显示出来 计时器6秒执行一次
    var setintervalFunction = setInterval(function() {
      that.getChatList();
      wx.hideNavigationBarLoading()
    }, 6000) //6秒查询一次
    that.setData({
      setinterval: setintervalFunction
    })
    var openid = wx.getStorageSync('openid');
    app.util.request({
      'url': 'entry/wxapp/Myzhuan',
      data: {
        openid: openid
      },
      success: function (res) {
        //console.log(res)
        that.setData({
          myzhuan: res.data.data,
        })
      },
      fail: function (err) {
        console.log(err)
      },

    })

  },

  //获得好友列表移机提醒消息
  getChatList: function() {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/KfuList',
      'data': {
        openid: wx.getStorageSync('openid'),
      },
      success: function(res) {
        console.log(res)
        var info = res.data.data
        that.setData({
          list: info
        })
      },
    })
  },
  getDaohang: function() {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Daohang',
      success: function(res) {
        var footer = that.data.footer;
        footer.list = res.data.data;
        that.setData({
          footer: footer,
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 生命周期函数--监听页面卸载
    //console.log(" onUnload");
    //清除 自动查询计时器
    var that = this;
    clearInterval(that.data.setinterval);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    // console.log('hide')
    // var that = this;
    // clearInterval(that.data.setinterval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})